﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.Security
{
    public class RMS_PermissionValueCollection : IEnumerable<RMS_PermissionItemValue>
    {
        private object _lockerObj=new object();
        private Dictionary<int,RMS_PermissionItemValue>  _idList=new Dictionary<int, RMS_PermissionItemValue>();
        private Dictionary<string,RMS_PermissionItemValue> _nameList=new Dictionary<string, RMS_PermissionItemValue>();
        private IEnumerable<RMS_PermissionItemValue> _permissionList = null;

        /// <summary>
        ///     
        /// </summary>
        /// <param name="permissionList">总的权限子项列表</param>
        public RMS_PermissionValueCollection(IEnumerable<RMS_PermissionItemValue> permissionList)
        {
            _permissionList = permissionList;
        }

        /// <summary>
        ///     添加一个权限子项到该权限容器中，如果本身已存在该子项，则自动修改子项值<br/>
        ///     注意：新增的权限子项，必须存在于permissionList指定的列表中
        /// </summary>
        /// <param name="permissionID"></param>
        /// <param name="controlValue"></param>
        public void Add(int permissionID,int controlValue)
        {
            lock (_lockerObj)
            {
                RMS_PermissionItemValue permissionItem = null;

                if (_idList.TryGetValue(permissionID, out permissionItem))
                {
                    permissionItem.ControlValue = controlValue;

                    _nameList[permissionItem.PermissionName].ControlValue = controlValue;
                }
                else
                {
                    var item = _permissionList.FirstOrDefault((s) => s.PermissionID == permissionID);

                    if (item!=null)
                    {
                        var newItem = RMS_PermissionItemValue.Load(item);

                        _idList.Add(permissionID, newItem);
                        _nameList.Add(item.PermissionName, newItem);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
        }
        
        /// <summary>
        ///     按ID移除权限子项
        /// </summary>
        /// <param name="permissionID"></param>
        public void Remove(int permissionID)
        {
            lock (_lockerObj)
            {
                RMS_PermissionItemValue item = null;

                if (_idList.TryGetValue(permissionID, out item))
                {
                    _idList.Remove(permissionID);
                    _nameList.Remove(item.PermissionName);
                }
            }
        }


        /// <summary>
        ///     清空本权限列表
        /// </summary>
        public void Clear()
        {
            lock (_lockerObj)
            {
                _idList.Clear();
                _nameList.Clear();
            }
        }

        /// <summary>
        ///     判断是否拥有某个ID的权限
        /// </summary>
        /// <param name="permissionID">权限子项的ID值</param>
        /// <returns></returns>
        public bool HasPermission(int permissionID)
        {
            RMS_PermissionItemValue item = null;

            if (_idList.TryGetValue(permissionID,out item))
            {
                return item.ControlValue == 0;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///     判断是否拥有某个ID的权限
        /// </summary>
        /// <param name="permissionName">权限子项的Name值</param>
        /// <returns></returns>
        public bool HasPermission(string permissionName)
        {
            RMS_PermissionItemValue item = null;

            if (_nameList.TryGetValue(permissionName, out item))
            {
                return item.ControlValue == 0;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///     获取当前用户的指定ID的权限子项值。<br/>
        ///     如果不存在该权限，则返回0；如果存在，则返回该子项的值
        /// </summary>
        /// <param name="permissionID"></param>
        /// <returns></returns>
        public int GetPermissionValue(int permissionID)
        {
            RMS_PermissionItemValue item = null;

            if (_idList.TryGetValue(permissionID, out item))
            {
                return item.ControlValue;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        ///     获取当前用户的指定ID的权限子项值。<br/>
        ///     如果不存在该权限，则返回0；如果存在，则返回该子项的值
        /// </summary>
        /// <param name="permissionID"></param>
        /// <returns></returns>
        public int GetPermissionValue(string permissionName)
        {
            RMS_PermissionItemValue item = null;

            if (_nameList.TryGetValue(permissionName, out item))
            {
                return item.ControlValue;
            }
            else
            {
                return 0;
            }
        }

        #region Implementation of IEnumerable

        public IEnumerator<RMS_PermissionItemValue> GetEnumerator()
        {
            foreach (var itemValue in _idList)
            {
                yield return itemValue.Value;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
